import 'dart:developer';

import 'package:FlutterDemo/common/commons.dart';
import 'package:FlutterDemo/widget/customerviews/test_page_container.dart';
import 'package:FlutterDemo/widget/customerviews/test_page_fittedbox.dart';
import 'package:FlutterDemo/widget/customerviews/test_page_icon.dart';
import 'package:FlutterDemo/widget/customerviews/test_page_story.dart';
import 'package:flutter/material.dart';

class SampleMaterialAppWidget extends StatelessWidget {
  const SampleMaterialAppWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return buildTestPage2();
  }

  Widget buildTestPage1() {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text(
            "MaterialApp示例",
          ),
          centerTitle: true, // 文字居中
        ),
        body: _SampleMaterialAppWidget(),
      ),
    );
  }

  /// 不建议使用，数据更新不到标题栏上
  Widget buildTestPage2() {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // 主题相关
      theme: ThemeData(primaryColor: Colors.red),
      // 主题相关
      darkTheme: ThemeData(primaryColor: Colors.red),
      // 主题相关
      themeMode: ThemeMode.dark,
      // 主题相关
      title: "老孟",
      onGenerateTitle: (context) {
        log("onGenerateTitle");
        // 获取当前系统语言
        var local = Localizations.localeOf(context);
        log("local = $local");
        if (local.languageCode == 'zh') {
          return '老孟';
        }
        return 'laomeng';
      },
      routes: {
        // 生效路由
        'container': (context) => ContainerDemo(),
        'fitted': (context) => FittedBoxDemo(),
        'icon': (context) => IconDemo()
      },
      initialRoute: '/',
      // 初始化根路径
      onGenerateRoute: (RouteSettings routeSettings) {
        var routeName = routeSettings.name;
        log("routeName = $routeName");
        // 这里不要有/否则 匹配不上
        var newRouteName = routeName!.substring(1);
        log("newRouteName: $newRouteName");
        // 生成路由界面
        if ('icon' == newRouteName) {
          return MaterialPageRoute(builder: (context) {
            return buildBasicMaterialAppWidget("icon", IconDemo());
          });
        } else if ('fitted' == newRouteName) {
          return MaterialPageRoute(builder: (context) {
            return buildBasicMaterialAppWidget("fitted", FittedBoxDemo());
          });
        } else if ("storytest" == newRouteName) {
          return MaterialPageRoute(builder: (context) {
            final StoryArguments arguments = routeSettings.arguments as StoryArguments;
            return TestPageStory(
              title: arguments.title,
              msg: arguments.msg,
            );
          });
        } else {
          return MaterialPageRoute(builder: (context) {
            return buildBasicMaterialAppWidget("container", ContainerDemo());
          });
        }
      },
      home: Scaffold(
        appBar: AppBar(
          title: Text("老孟"),
          centerTitle: true,
        ),
        body: _SampleMaterialAppWidget(),
      ),
    );
  }
}

class _SampleMaterialAppWidget extends StatefulWidget {
  const _SampleMaterialAppWidget({Key? key}) : super(key: key);

  @override
  _SampleMaterialAppWidgetState createState() => _SampleMaterialAppWidgetState();
}

class _SampleMaterialAppWidgetState extends State<_SampleMaterialAppWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          RaisedButton(
            child: Text("icon"),
            onPressed: () {
              Navigator.pushNamed(
                context,
                '/icon', // 跳转路径要加/否则 会失败
              );
            },
          ),
          RaisedButton(
            child: Text("container"),
            onPressed: () {
              Navigator.pushNamed(
                context,
                '/container',
              );
            },
          ),
          RaisedButton(
            child: Text("fitted"),
            onPressed: () {
              Navigator.pushNamed(
                context,
                '/fitted',
              );
            },
          ),
          RaisedButton(
            child: Text("特定路由"),
            onPressed: () {
              Navigator.pushNamed(context, '/storytest',
                  arguments: StoryArguments(
                      "Accept Arguments Screen", "This message is extracted in the onGenerateRoute function."));
            },
          ),
          Expanded(
              // 默认点满剩余空间
              child: Center(
            child: Text("功能未完整"),
          ))
        ],
      ),
    );
  }
}
